package cn.edu.stu.bean;

import cn.edu.stu.tool.MyHttpStatus;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * @author Karl
 * @Date 2022/12/11 - 19:49
 */
public class AjaxResult {

    private int code;

    private String msg;

    private HashMap<String,Object> data;

    public static AjaxResult success(){
        return AjaxResult.success("操作成功");
    }

    public static AjaxResult success(String msg){
        return AjaxResult.success(msg,null);
    }

    public static AjaxResult success(String msg,Object data){
        return new AjaxResult(MyHttpStatus.SUCCESS,msg,data);
    }

    public AjaxResult(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = new HashMap<>();
        if (data != null) {
            this.data.put("data",data);
        }
    }

    public AjaxResult() {
    }

    public static AjaxResult error(){
        return AjaxResult.success("操作失败");
    }

    public static AjaxResult error(String msg){
        return AjaxResult.error(msg,null);
    }

    public static AjaxResult error(String msg,Object data){
        return new AjaxResult(MyHttpStatus.FAIL,msg,data);
    }


    public void put(String key, Object value){
        data.put(key,value);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData(String key) {
        return data.get(key);
    }

    public HashMap<String, Object> getData() {
        return data;
    }

    public void setData(HashMap<String, Object> data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "AjaxResult{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
